﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using JohnnySins.DAL;
using JohnnySins.DAL.Repositories;
using JohnnySins.Entities;
using JohnnySins.Code;

namespace JohnnySins
{
    public partial class StarProfileDetails : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                LoadData();
        }

        private void LoadData()
        {
            using (var repo = RepositoryFactory.ResolveRepository<StarProfileRepository>())
            {
                StarProfile itm = repo.Find(StarProfileId, true);
                if (itm == null)
                {
                    AlertAndClear("Profile not found!");
                    return;
                }

                lblFullName.Text = lblAbout.Text = lblSceneOwner.Text = itm.FullName;
                lblScenesCount.Text = itm.Scenes==null?"00":itm.Scenes.Count().ToString("00");
                lblEyeColor.Text = string.IsNullOrEmpty(itm.EyeColor) ? "Unknown" : itm.EyeColor;
                lblHairColor.Text = string.IsNullOrEmpty(itm.HairColor)?"Unknown":itm.HairColor;
                lblDOB.Text = itm.DateOfBirth.HasValue ? itm.DateOfBirth.Value.ToString("MMMM dd, yyyy") : "Unknown";
                lblPlaceOfBirth.Text = string.IsNullOrEmpty(itm.PlaceOfBirth) ? "Unknown" : itm.PlaceOfBirth;
                lblEthnicity.Text = string.IsNullOrEmpty(itm.Ethnicity) ? "Unknown" : itm.Ethnicity;
                lblWeight.Text = string.IsNullOrEmpty(itm.Weight) ? "Unknown" : itm.Weight;
                lblHeight.Text = string.IsNullOrEmpty(itm.Height) ? "Unknown" : itm.Height;
                lblLikes.Text = itm.Likes.ToString();
                lblDislikes.Text = itm.Dislikes.ToString();
                if (itm.Scenes != null)
                {
                    rptScene.DataSource = itm.Scenes;
                    rptScene.DataBind();
                }

                if ((itm.Photos != null) && (itm.Photos.Count() > 0))
                {
                    Random rnd = new Random();
                    imgProfilePhoto.Src = string.Format("~/Files/StarProfile/{0}/Photo/{1}", itm.StarProfileId.ToString(), itm.Photos.ToArray()[rnd.Next(0, itm.Photos.Count() - 1)].PhysFileName);
                }
                else
                    imgProfilePhoto.Visible = false;
            }
        }

        protected void rptScene_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {

        }

        protected void rptScene_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if ((e.CommandName == "Like") || (e.CommandName == "Dislike"))
            {
                int id = Convert.ToInt32(e.CommandArgument);
                bool isLiked = (e.CommandName == "Like");
                string ip = GetIPAddress();

                using (var repo = RepositoryFactory.ResolveRepository<StarProfileRepository>())
                {
                    repo.LikeScene(id, ip, isLiked);
                }
                LoadData();
            }
        }


        private int StarProfileId
        {
            get
            {
                if (ViewState["StarProfileId"] == null)
                {
                    int id = -1;

                    if (Request.QueryString["pid"] == null)
                        return -1;

                    if (!string.IsNullOrEmpty(Request.QueryString["pid"].ToString()))
                    {
                        if (int.TryParse(Request.QueryString["pid"].ToString(), out id))
                            return id;
                        else
                            return -1;
                    }
                    else
                        return -1;
                }
                else
                    return Convert.ToInt32(ViewState["StarProfileId"]);
            }
            set
            {
                ViewState["StarProfileId"] = value;
            }
        }
    }
}